<template>
  <div class="scan-page">
    <van-nav-bar
      title="扫一扫"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    
    <div class="scan-content">
      <div class="scan-area">
        <div class="scan-frame">
          <div class="scan-line"></div>
          <div class="scan-angle scan-angle-lt"></div>
          <div class="scan-angle scan-angle-rt"></div>
          <div class="scan-angle scan-angle-lb"></div>
          <div class="scan-angle scan-angle-rb"></div>
        </div>
        <div class="scan-tip">将二维码/条形码放入框内，即可自动扫描</div>
      </div>
      
      <div class="scan-actions">
        <div class="action-item" @click="openAlbum">
          <van-icon name="photo-o" size="24" />
          <span>从相册选取</span>
        </div>
      </div>
    </div>
    
    <!-- 扫码结果弹窗 -->
    <van-popup v-model:show="showResultPopup" round :style="{ width: '80%' }">
      <div class="result-popup">
        <div class="popup-header">
          <span>扫码结果</span>
          <van-icon name="cross" @click="showResultPopup = false" />
        </div>
        
        <div class="result-content">
          <div class="result-type">{{ scanResult.type === 'url' ? '网址' : '文本' }}</div>
          <div class="result-text">{{ scanResult.content }}</div>
        </div>
        
        <div class="result-actions">
          <van-button 
            v-if="scanResult.type === 'url'" 
            type="primary" 
            block 
            @click="openUrl(scanResult.content)"
          >
            打开链接
          </van-button>
          <van-button 
            v-else 
            type="primary" 
            block 
            @click="copyText(scanResult.content)"
          >
            复制文本
          </van-button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'

const router = useRouter()
const showResultPopup = ref(false)

// 当前扫码结果
const scanResult = ref({
  content: '',
  type: 'text'
})

// 返回上一页
const onClickLeft = () => {
  router.back()
}

// 打开相册
const openAlbum = () => {
  showToast('打开相册')
  // 模拟从相册中选择图片并识别二维码
  setTimeout(() => {
    scanResult.value = {
      content: 'https://example.com/scan-demo',
      type: 'url'
    }
    showResultPopup.value = true
  }, 1000)
}

// 打开URL
const openUrl = (url) => {
  window.open(url, '_blank')
  showToast('正在打开链接')
  showResultPopup.value = false
}

// 复制文本
const copyText = (text) => {
  navigator.clipboard.writeText(text).then(() => {
    showToast('已复制到剪贴板')
    showResultPopup.value = false
  }).catch(() => {
    showToast('复制失败')
  })
}
</script>

<style lang="scss" scoped>
.scan-page {
  min-height: 100vh;
  background-color: #000;
  color: #fff;
  
  .scan-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    height: calc(100vh - 46px - var(--tabbar-height));
    padding: 20px;
    
    .scan-area {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      
      .scan-frame {
        position: relative;
        width: 250px;
        height: 250px;
        margin-bottom: 20px;
        
        .scan-line {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 2px;
          background-color: var(--primary-color);
          animation: scan-animation 2s linear infinite;
        }
        
        .scan-angle {
          position: absolute;
          width: 20px;
          height: 20px;
          border-color: var(--primary-color);
          
          &.scan-angle-lt {
            top: 0;
            left: 0;
            border-top: 3px solid;
            border-left: 3px solid;
          }
          
          &.scan-angle-rt {
            top: 0;
            right: 0;
            border-top: 3px solid;
            border-right: 3px solid;
          }
          
          &.scan-angle-lb {
            bottom: 0;
            left: 0;
            border-bottom: 3px solid;
            border-left: 3px solid;
          }
          
          &.scan-angle-rb {
            bottom: 0;
            right: 0;
            border-bottom: 3px solid;
            border-right: 3px solid;
          }
        }
      }
      
      .scan-tip {
        font-size: 14px;
        color: rgba(255, 255, 255, 0.8);
        text-align: center;
      }
    }
    
    .scan-actions {
      display: flex;
      justify-content: center;
      width: 100%;
      padding: 20px 0 40px;
      
      .action-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        
        span {
          margin-top: 8px;
          font-size: 12px;
          color: rgba(255, 255, 255, 0.8);
        }
      }
    }
  }
  
  .result-popup {
    padding: 16px;
    
    .popup-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-bottom: 16px;
      border-bottom: 1px solid var(--border-color-light);
      
      span {
        font-size: 16px;
        font-weight: 500;
      }
    }
    
    .result-content {
      padding: 16px 0;
      
      .result-type {
        font-size: 14px;
        color: var(--text-color-secondary);
        margin-bottom: 8px;
      }
      
      .result-text {
        font-size: 16px;
        color: var(--text-color);
        word-break: break-all;
      }
    }
    
    .result-actions {
      margin-top: 16px;
    }
  }
}

@keyframes scan-animation {
  0% {
    top: 0;
  }
  50% {
    top: 100%;
  }
  100% {
    top: 0;
  }
}
</style> 